﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using QuickTasks.ViewModels.Data;
using Microsoft.Advertising.Mobile.UI;

namespace QuickTasks
{
    public partial class AddLinks : PhoneApplicationPage
    {
       
        List<string> str1 = new List<string>();
        string linkid = null;
        #region constructor

        public AddLinks()
        {
            InitializeComponent();
          
            Loaded += new RoutedEventHandler(AddLinks_Loaded);
        }

        void AddLinks_Loaded(object sender, RoutedEventArgs e)
        {
            int j = 1;
            var textBoxes = new[] { tbxLink1, tbxLink2, tbxLink3, tbxLink4, tbxLink5, tbxLink6 };
            try
            {
                if (NavigationContext.QueryString.TryGetValue("linkid", out linkid))
                {
                    using (QuickTaskDataContext context = new QuickTaskDataContext(App.ConnectionString))
                    {
                        ApplicationTitle.Text = "QUICK TASKS - EDIT LINKS";
                        var listquery = from i in context.link where i.id.ToString() == linkid select i;
                        foreach (var objlist in listquery)
                        {
                            tbxName.Text = objlist.name;
                            str1=retrive(objlist.linkelement);
                            int count = str1.Count;
                         while(j<count+1)
                            {
                                textBoxes[j - 1].Text = str1[j - 1].ToString();
                                j++;
                            }
                            break;
                        }
                    }
                }
                else
                {
                    ApplicationTitle.Text = "QUICK TASKS - ADD LINKS";
                }
            }
            catch (Exception ex)
            {
                Exceptions.SaveOrSendExceptions("Exception in AddLinks_Loaded event In AddLinks.xaml page", ex);
            }
        }
        
        #endregion

        #region Events
      
        private void ApplicationBarsaveButton_Click(object sender, EventArgs e)
        {

            try
            {
                if (tbxName.Text == "")
                {
                    tblkNameValidation.Visibility = Visibility.Visible;
                    return;
                }
                else
                    tblkNameValidation.Visibility = Visibility.Collapsed;

                if (tbxLink1.Text == "")
                {
                    tblkLinkValidation.Visibility = Visibility.Visible;
                    return;
                }
                else
                    tblkLinkValidation.Visibility = Visibility.Collapsed;

                QuickLinks objQuickLinks = new QuickLinks();
                objQuickLinks.SaveLinks(tbxName.Text, tbxLink1.Text, tbxLink2.Text, tbxLink3.Text, tbxLink4.Text, tbxLink5.Text, tbxLink6.Text, linkid);
                NavigationService.GoBack();
            }
            catch (Exception ex)
            {
                Exceptions.SaveOrSendExceptions("Exception in ApplicationBarsaveButton_Click event In AddLinks.xaml page", ex);
            }
        }
        public List<string> retrive(string s)
        {
            List<string> str = new List<string>();

            string[] Emails = s.Split(new char[] { ',', ';' });
            foreach (string str1 in Emails)
            {
                str.Add(str1);
            }
            return str;

        }
    }
}
        #endregion